package com.meritdata.cloud.data.service.development.controller;

import com.meritdata.cloud.data.service.development.model.TdDataApiResultFiled;
import com.meritdata.cloud.data.service.development.model.TdDataApiTable;
import com.meritdata.cloud.data.service.development.service.DataApiResultFiledService;
import com.meritdata.cloud.data.service.development.service.DataApiTableService;
import com.meritdata.cloud.resultmodel.ResultBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@RequestMapping("/apiResult")
@Controller
public class DataApiResultFiledController {

    @Autowired
    private DataApiResultFiledService service;
    @RequestMapping("/create")
    @ResponseBody
    public ResultBody create(@RequestParam String apiId,@RequestParam String ids ) {
        ResultBody r = new ResultBody();
        service.deleteByApiId(Long.valueOf(apiId));
        String[] cloumns = ids.split(",");
        for(int i = 0;i < cloumns.length ; i++){
            TdDataApiResultFiled d = new TdDataApiResultFiled();
            d.setApiId(Long.valueOf(apiId));
            d.setUpdateTime(new Date());
            d.setCreateTime(new Date());
            d.setCloumnId(Long.valueOf(cloumns[i]));
            service.save(d);
        }
        r.setData(true);
        r.setSuccess(true);
        return r;
    }
    @RequestMapping("/findByApiId")
    @ResponseBody
    public ResultBody findByApiId( @RequestParam String apiId ) {
        ResultBody r = new ResultBody();
        r.setData(service.findByApiId(Long.valueOf(apiId)));
        r.setSuccess(true);
        return r;
    }
    @RequestMapping("/delByApiId")
    @ResponseBody
    public ResultBody delByApiId( @RequestParam String apiId ) {
        ResultBody r = new ResultBody();
        r.setData(service.deleteByApiId(Long.valueOf(apiId)));
        r.setSuccess(true);
        return r;
    }
}